//
//  CellModel.m
//  PlanDemo
//
//  Created by 在成长开发 on 2025/9/8.
//

#import "CellModel.h"
#import <MJExtension.h>

@implementation CellModel

- (instancetype)init {
    self = [super init];
    if (self) {
        //
    }
    return self;
}

- (BOOL)canDrag {
    return self.type == CellType_Activity;
}

- (BOOL)canDropFrom:(CellModel *)other {
    return self.type == CellType_Activity && other.type == CellType_Activity;
}

+ (NSMutableArray<CellModel *> *)fifteenWeek {
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"fifteen" ofType:@"json"];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
//    return [CellModel mj_objectArrayWithKeyValuesArray:data];
    return [[[CellModel mj_objectArrayWithKeyValuesArray:data] sortedArrayUsingComparator:^NSComparisonResult(CellModel *  _Nonnull obj1, CellModel *  _Nonnull obj2) {
        if (obj1.row > obj2.row) {
            return NSOrderedAscending;
        } else if (obj1.row < obj2.row) {
            return NSOrderedDescending;
        } else {
            if (obj1.col > obj2.col) {
                return NSOrderedAscending;
            } else if (obj1.col < obj2.col) {
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
            }
        }
    }] mutableCopy];
}

+ (NSMutableArray<CellModel *> *)thirteenWeek {
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"thirteen" ofType:@"json"];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    return [CellModel mj_objectArrayWithKeyValuesArray:data];
}

/// 生成和上一行相同合并样式的单元格
/// - Parameters:
///   - source: 数据源
///   - startRow: <#startRow description#>
///   - appendRow: <#appendRow description#>
+ (void)createItemsSameAsLast:(NSMutableArray *)source startRow:(NSInteger)startRow appendRow:(NSInteger)appendRow {
    // 找到上面一行所有的单元格
    NSMutableArray <CellModel *>* lastRowItems = [NSMutableArray array];
    NSInteger lasIndex = 0;
    for (CellModel * obj in source) {
        if (obj.row + obj.useRow == startRow) {
            [lastRowItems addObject:obj];
            lasIndex = [source indexOfObject:obj];
        }
    }
    // 上面一行col从小到大重新排序
    [lastRowItems sortUsingComparator:^NSComparisonResult(CellModel *  _Nonnull obj1, CellModel *  _Nonnull obj2) {
        return obj1.col < obj2.col ? NSOrderedDescending : NSOrderedAscending;
    }];
    
    NSMutableArray <CellModel *>* result = [NSMutableArray array];
    for (CellModel * obj in lastRowItems) {
        CellModel * model = [[CellModel alloc] init];
        model.type = obj.type;
        model.row = startRow;
        model.useRow = appendRow;
        model.col = obj.col;
        model.useCol = obj.useCol;
        [result addObject:model];
    }
    
    // 插入到lastIndex后面
    for (CellModel * obj in result.reverseObjectEnumerator) {
        [source insertObject:obj atIndex:lasIndex];
    }
}

/// 生成打散单元格的样式的行
/// - Parameters:
///   - source: 数据源
///   - startRow: <#startRow description#>
///   - appendRow: <#appendRow description#>
+ (void)createItemsNoMerge:(NSMutableArray *)source startRow:(NSInteger)startRow appendRow:(NSInteger)appendRow {
    // 找到上面一行所有的单元格
    NSMutableArray <CellModel *>* lastRowItems = [NSMutableArray array];
    NSInteger lasIndex = 0;
    for (CellModel * obj in source) {
        if (obj.row + obj.useRow == startRow) {
            [lastRowItems addObject:obj];
            lasIndex = [source indexOfObject:obj];
        }
    }
    // 上面一行col从小到大重新排序
    [lastRowItems sortUsingComparator:^NSComparisonResult(CellModel *  _Nonnull obj1, CellModel *  _Nonnull obj2) {
        return obj1.col < obj2.col ? NSOrderedDescending : NSOrderedAscending;
    }];
    
    NSMutableArray <CellModel *>* result = [NSMutableArray array];

    for (CellModel * obj in lastRowItems) {
        if (obj.type == CellType_Activity) {
            CellModel * model = [[CellModel alloc] init];
            model.type = obj.type;
            model.row = startRow;
            model.useRow = appendRow;
            model.col = obj.col;
            model.useCol = obj.useCol;
            [result addObject:model];
        } else {
            for (NSInteger i = obj.col; i < obj.col + obj.useCol; i++) {
                CellModel * model = [[CellModel alloc] init];
                model.type = obj.type;
                model.row = startRow;
                model.useRow = appendRow;
                model.col = i;
                model.useCol = 1;
                [result addObject:model];
            }
        }
        
    }
    
    // 插入到lastIndex后面
    for (CellModel * obj in result.reverseObjectEnumerator) {
        [source insertObject:obj atIndex:lasIndex];
    }
}

+ (void)updateCells:(NSMutableArray <CellModel *>*)result noon:(CellModel *)noon afterRow:(NSInteger)startRow appendRow:(NSInteger)appendRow {
    noon.useRow += appendRow;
    for (CellModel * model in result) {
        if (model == noon) {
            continue;
        }
        
        if (model.row >= startRow) {
            model.row += appendRow;
        }
        if (model.row <= noon.row && (model.row + model.useRow) >= (noon.row + noon.useRow - appendRow)) {
            model.useRow += appendRow;
        }
    }
}

@end
